<?php

namespace App\Console\Commands;

use App\Jobs\Viewer;
use App\Models\Position;
use App\Services\CrawlerService\CrawlerService;
use App\Services\CrawlerService\driver\Xinxin21ccnnDriver;
use App\Services\LbsService\LbsService;
use App\Services\TagService\TagService;
use Illuminate\Console\Command;

class UpdateGeo extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'update_geo {ext?}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $ext = $this->argument('ext');
        if(is_numeric($ext)){
            $positions = Position::whereId($ext)->get();
        } elseif ($ext == 'force') {
            $positions = Position::whereNotNull('address')->get();
        } else {
            $positions = Position::whereNotNull('address')->whereNull('geo')->get();
        }
        $lbs=new LbsService();
        $bar=$this->output->createProgressBar(count($positions));
        foreach ($positions as $position) {
            $geo=$lbs->address("广东省云浮市新兴县".$position->address);
            if($geo->toPoint()){
                $position->geo=$geo->toGeo();
                list($lat,$lng)=$geo->toPoint();
                $position->lat=$lat;
                $position->lng=$lng;
                $position->save();
            }
            $bar->advance();
            sleep(1);
        }
        $bar->finish();
        $this->info('更新完成');
    }
}
